package com.example.springboot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.dao.GradeMapper;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.Grade;
import com.example.springboot.domain.po.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.AdminService;
import com.example.springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.controller
 * @Project：springboot
 * @name：UserController
 * @Date：2025/2/4 21:20
 * @Filename：UserController
 * @for:
 */
@Api(tags = "用户管理接口")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private GradeMapper gradeMapper;

    /**
     * 查询用户接口
     *
     * @return 返回用户列表
     */
    @ApiOperation("查询用户接口")
    @GetMapping("/alldata")
    public Result getData() {
        List<User> userList = userService.getAll();
        if (userList.size() < 1) {
            throw new CustomException(ResultCode.DATA_USER_LESS);
        }
        return Result.success(userList);
    }

    /**
     * 通过用户id查询用户接口
     *
     * @return 返回用户
     */
    @ApiOperation("查询用户接口")
    @GetMapping("/getById")
    public Result getData(Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }

    @ApiOperation("分页查询用户接口")
    @PostMapping("/page")
    public Result page(@RequestBody User search,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        return Result.success(userService.findPage(search, pageNum, pageSize));
    }

    @ApiOperation("新增用户接口")
    @PostMapping
    public Result userRegister(@RequestBody User user) {
        return Result.success(userService.userRegister(user));
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("修改用户接口")
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success(user);
    }

    @ApiOperation("修改角色权限接口")
    @PostMapping("/changeRole")
    public Result userRole(@RequestBody User user) {

        return Result.success(userService.userRole(user));

    }

    @ApiOperation("通过excel批量导入接口")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        List<User> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(User.class);
        if (!CollectionUtil.isEmpty(infoList)) {
            for (User info : infoList) {
                try {
                    userService.userRegister(info);
                } catch (Exception e) {
                    return Result.error("导入失败");
                }
            }
        }
        return Result.success();
    }


    @ApiOperation("批量导出到excel")
    @GetMapping("/export")
    public Result export(@RequestParam("gradeName") String gradeName, @RequestParam("username") String username, HttpServletResponse response) throws IOException {
        try {
            userService.export(gradeName, username, response);
            return Result.success();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("根据用户id导出用户信息到excel")
    @GetMapping("/exportById")
    public Result exportById(Integer id,  HttpServletResponse response) throws IOException {
        try {
            userService.exportById(id, response);
            return Result.success();
        } catch (IOException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 描述：批量删除
     */
    @PostMapping("/delBatch")
    public Result delBatch(@RequestBody List<User> list) {
        userService.deleteBatch(list);
        return Result.success();
    }


}